package com.shop.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.shop.dao.OrderDao;
import com.shop.domain.Order;
import com.shop.util.MyBatisUtil;

/**
 * 
 * @author wenjuanhenxing
 * @version V1.0 OrderDaoImpl.java 2016-2-11 下午5:46:48
 */
@Repository("orderDaoImpl")
public class OrderDaoImpl implements OrderDao {

	@Override
	public boolean addOrder(Order order) {
		if (order == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.insert("com.shop.dao.mapping.OrderMapper"
				+ ".addOrder", order);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean deleteOrderById(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.delete("com.shop.dao.mapping.OrderMapper"
				+ ".deleteOrderById", id);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateOrderStatus(Order order) {
		if (order == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.OrderMapper"
				+ ".updateOrderStatus", order);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateOrderCustomerEvaluate(Order order) {
		if (order == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.OrderMapper"
				+ ".updateOrderCustomerEvaluate", order);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateOrderSellerEvaluate(Order order) {
		if (order == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.OrderMapper"
				+ ".updateOrderSellerEvaluate", order);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public Order getOrderById(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Order order = sqlSession.selectOne("com.shop.dao.mapping.OrderMapper"
				+ ".getOrderById", id);
		MyBatisUtil.closeSession(sqlSession);
		return order;
	}

	@Override
	public List<Order> getOrderByCustomerId(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Order> orderList = sqlSession.selectList(
				"com.shop.dao.mapping.OrderMapper" + ".getOrderByCustomerId",
				id);
		MyBatisUtil.closeSession(sqlSession);
		return orderList;
	}

	@Override
	public List<Order> getOrderBySellerId(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Order> orderList = sqlSession.selectList(
				"com.shop.dao.mapping.OrderMapper" + ".getOrderBySllerId", id);
		MyBatisUtil.closeSession(sqlSession);
		return orderList;
	}

}
